//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2007
//
// Helpers for combobox control.
//


# include "StdAfx.h"
# include "ComboBox.h"
# include "Globals.h"
# include "ResMan.h"

void AddComboBox (HWND _hCB, size_t _cItem, LPARAM _lParam, const TCHAR * _strItem) {
	::SendMessage (_hCB, CB_INSERTSTRING, _cItem, (LPARAM) _strItem);
	::SendMessage (_hCB, CB_SETITEMDATA,  _cItem, (LPARAM) _lParam);
}

void AddComboBox (HWND _hCB, size_t _cItem, LPARAM _lParam, UINT _uiText) {
	TCHAR strBuf [128];
	LoadString (_uiText, strBuf, ARRAY_SIZE (strBuf));

	AddComboBox (_hCB, _cItem, _lParam, strBuf);
}

void SelectComboBox (HWND _hCB, size_t _cItem) {
	::SendMessage (_hCB, CB_SETCURSEL, _cItem, 0);
}

void FillComboBox (HWND _hWnd, UINT _uiCB, const CComboBoxData * _cbd, size_t _cItems, int _nInitCode) {
	const HWND hCB = ::GetDlgItem (_hWnd, _uiCB);
	for (size_t c = 0; c < _cItems; ++ c) {
		TCHAR strBuf [64];
		LoadString (_cbd [c].uiText, strBuf, sizeof (strBuf)/sizeof (strBuf [0]));

		AddComboBox (hCB, c, _cbd [c].nCode, strBuf);

		if (_cbd [c].nCode == _nInitCode)
			SelectComboBox (hCB, c);
	}
}

bool GetComboBoxItemData (HWND _hWnd, UINT _uiCB, int & _nCode) {
	const HWND hCB = ::GetDlgItem (_hWnd, _uiCB);
	const size_t cItemSel = ::SendMessage (hCB, CB_GETCURSEL, 0, 0);
	if (cItemSel == CB_ERR)
		return false;

	_nCode = ::SendMessage (hCB, CB_GETITEMDATA, cItemSel, 0);
	return true;
}
